package com.base.controller;

import com.base.bloom.RedisBloomFilter;
import com.base.bloom.BloomFilterHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RedisController {
    @Autowired
    private RedisBloomFilter redisBloomFilter;
    @Autowired
    private BloomFilterHelper bloomFilterHelper;

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping(value = "/ad")
    public String addBloomFiltera(String id){
        try {
            redisTemplate.opsForValue().setBit("v1", Long.parseLong(id),true);
        }catch (Exception e){
            e.printStackTrace();
            return "添加失败";
        }
        return "添加成功";
    }

    /***
     * 添加到redis
     * @param id
     * @return
     */
    @RequestMapping(value = "/add")
    public String addBloomFilter(String id){
        try {
            redisBloomFilter.addByBloomFilter(bloomFilterHelper,"bloom",id);
        }catch (Exception e){
            e.printStackTrace();
            return "添加失败";
        }
        return "添加成功";
    }

    /***
     * 查询是否存在
     * @param id
     * @return
     */
    @RequestMapping(value = "/check")
    public boolean checkBloomFilter(String id){
        boolean b = redisBloomFilter.includeByBloomFilter(bloomFilterHelper,"bloom",id);
        return b;
    }
}